﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Domain.ActiveDirectory.Model;
using Domain.Context;
using System.Security;
using System.Security.Principal;

namespace Domain.Provider
{

    public class DatabaseProvider :
        IDataProvider<NMCTerritory>,
        IDataProvider<Store>,
        IDataProvider<AccountType>,
        IDataProvider<ManagerHours>,
        IDataProvider<I9Document>,
        IDataProvider<StoreHours>
    {
        //private readonly string _devTestConnectionString = "Data Source=localhost;Initial Catalog=DevTest;Integrated Security=True";
        private readonly string _devTestConnectionString = "Data Source=laredo.rubios.com;Initial Catalog=DevTest;Persist Security Info=True;User ID=t-aveldhousen;Password=Veldhousen#44";
        //private readonly string _laredoConnectionString = "Data Source=laredo;Password=storedProc2012;User ID=StoredProcUser;Initial Catalog=DevTest";
        private RubiosDataContextDataContext _context;

        public DatabaseProvider()
        { 
            _context = new RubiosDataContextDataContext(_devTestConnectionString);
        }

        public void SaveEntity(AccountType entity, IPrincipal user)
        {
            _context.SetAccountType(entity.NMC_AccountTypeID, entity.NMC_AccountTypeName, user.Identity.Name, entity.Active);

            _context.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
            _context.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, _context.GetAccountTypes());
        }

        public void DeleteEntity(AccountType entity, IPrincipal user)
        {
            entity.Active = false;
            _context.SetAccountType(entity.NMC_AccountTypeID, entity.NMC_AccountTypeName, user.Identity.Name, entity.Active);

            _context.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
            _context.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, _context.GetAccountTypes());
        }

        public void SaveEntity(Store entity, IPrincipal user)
        {
            _context.UpdateStoreAssignment(entity.NMC_UID, entity.StoreID, user.Identity.Name);

            _context.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
            _context.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, _context.GetStores());
        }

        public void DeleteEntity(Store entity, IPrincipal user)
        { throw new InvalidOperationException("A store cannot be deleted."); }

        public void SaveEntity(NMCTerritory entity, IPrincipal user)
        {
            _context.AddArea(entity.NMC_UID, entity.NMC_Area, entity.NMC_PersonAcct, entity.NMC_GL_Acct, entity.NMC_AccountType, user.Identity.Name);

            _context.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
            _context.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, _context.GetAreas());
        }

        public void DeleteEntity(NMCTerritory entity, IPrincipal user)
        {
            _context.DeleteArea(entity.NMC_UID);
            
            _context.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
            _context.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, _context.GetAreas());
        }

        public void SaveEntity(ManagerHours entity, IPrincipal user)
        {
            throw new NotImplementedException();
        }

        public void DeleteEntity(ManagerHours entity, IPrincipal user)
        {
            throw new NotImplementedException();
        }

        IEnumerable<AccountType> IDataProvider<AccountType>.Entities
        { get { return _context.GetAccountTypes().ToList(); } }

        IEnumerable<Store> IDataProvider<Store>.Entities
        { get { return _context.GetStores().ToList(); } }

        IEnumerable<NMCTerritory> IDataProvider<NMCTerritory>.Entities
        { get { return _context.GetAreas().ToList(); } }

        IEnumerable<ManagerHours> IDataProvider<ManagerHours>.Entities
        {
            get { return new ManagerHours[0]; }
        }

        #region IDataProvider<I9Document> Members

        IEnumerable<I9Document> IDataProvider<I9Document>.Entities
        {
            get { return _context.GetI9Documents(); }
        }

        public void SaveEntity(I9Document entity, IPrincipal user)
        {
            throw new NotImplementedException();
        }

        public void DeleteEntity(I9Document entity, IPrincipal user)
        {
            throw new NotImplementedException();
        }

        #endregion
        
        #region IDataProvider<StoreHours> Members

        IEnumerable<StoreHours> IDataProvider<StoreHours>.Entities
        {
            get { return _context.GetStoreHours(); }
        }

        public void SaveEntity(StoreHours entity, IPrincipal user)
        {
            throw new NotImplementedException();
        }

        public void DeleteEntity(StoreHours entity, IPrincipal user)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
